/*
* $Id: JournalTreeView.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#pragma warning(disable:4312)
#include "Common/include.h"
#include "Components/Journal/Styles.h"
#include "Components/Journal/FamilyManager.h"
#include "Components/Journal/InspectManager.h"
#include "Components/Journal/QueenBeeManager.h"
#include "JournalTreeView.h"
#include "ViewJournal.h"
#include "../CommandIds.h"

CJournalTreeView::CJournalTreeView()
: CJournalView(new CTreeStyle){
	m_pViewJornal = NULL;
	m_viewFactory.Init(&m_state);
}
CJournalTreeView::~CJournalTreeView(){
	if(m_pViewJornal)
		m_pViewJornal->DestroyView();
}

void CJournalTreeView::CreateView(int hwnd){
	m_hWnd = (HWND)hwnd;
	CWnd* pWnd = CWnd::FromHandle((HWND)hwnd);
	m_tabCtrl.WindowCreate(pWnd);
	m_pViewJornal = m_viewFactory.GetViewJournal(State(Journal::INSPECT_JOURNAL,
		Journal::NOTSEL, Journal::NONE));
	m_pViewJornal->CreateView();
	m_NavigationWin.WindowCreate(pWnd);
	CRect rect;
	GetClientRect(m_hWnd, rect);
	m_sizeWin = rect.Size();
}

BEGIN_MY_MESSAGE_MAP(CJournalTreeView)
	ON_WND_MSG(WM_SIZE, OnSize)
	ON_WND_MSG(WM_SETFOCUS, OnSetFocus)
	ON_WND_MSG(UM_ITEM_MANIPULATION, OnItemManipulation)
	ON_NOTIFY_MSG(TCN_SELCHANGE, JOURNAL_TAB_CTRL_ID, OnTabSelChange)
	ON_NOTIFY_MSG(TVN_SELCHANGED, TREE_NAVIGATION_WND_ID, OnTreeSelChange)
	ON_NOTIFY_MSG(TVN_ENDLABELEDIT, TREE_NAVIGATION_WND_ID, OnEndItemEdit)
END_MY_MESSAGE_MAP()

long CJournalTreeView::OnSize(unsigned int wParam, long lParam){
	m_sizeWin.SetSize(LOWORD(lParam), HIWORD(lParam) - SIZE_TOOLBAR);
	m_tabCtrl.MoveWindow(200, SIZE_TOOLBAR, LOWORD(lParam) - 200, 24);
	m_NavigationWin.MoveWindow(0, SIZE_TOOLBAR, 200, HIWORD(lParam) - SIZE_TOOLBAR);
	m_pViewJornal->Resize(200, 24 + SIZE_TOOLBAR, LOWORD(lParam) - 200, HIWORD(lParam) - 24 - SIZE_TOOLBAR);
	return 0;
}
long CJournalTreeView::OnSetFocus(unsigned int wParam, long lParam){
	m_pViewJornal->Focus();
	return 0;
}
long CJournalTreeView::OnTabSelChange(LPNMHDR pInfo){
	SelChange();
	return 0;
}
long CJournalTreeView::OnTreeSelChange(LPNMHDR pInfo){
	SelChange();
	return 0; 
}
long CJournalTreeView::OnEndItemEdit(LPNMHDR pInfo){
	LPNMTVDISPINFO info = (LPNMTVDISPINFO)pInfo;
	std::tstring strItemText = m_NavigationWin.GetItemText(info->item.hItem).GetString();
	if(!info->item.pszText || strItemText == info->item.pszText || !_tcslen(info->item.pszText))
		return 0;
	m_NavigationWin.SetItemText(info->item.hItem, info->item.pszText);
	long p = (long)m_NavigationWin.GetItemData(info->item.hItem);
	if(LOWORD(p) == Journal::POINT)
		IJournalComponent::GetFamilyManager()->RenamePoint(HIWORD(p), info->item.pszText);
	else if(LOWORD(p) == Journal::FAMILY)
		IJournalComponent::GetFamilyManager()->RenameFamily(HIWORD(p), info->item.pszText);
	return 0;
}
long CJournalTreeView::OnItemManipulation(unsigned int wParam, long lParam){
	ItemManipulation((Journal::typeItemMnpl)wParam, lParam);
	return 0;
}

void CJournalTreeView::ItemManipulation(Journal::typeItemMnpl type, int param){
	if(type == Journal::DELETE_POINT)
		IJournalComponent::GetFamilyManager()->DeletePoint(param);
	else if(type == Journal::DELETE_FAMILY)
		IJournalComponent::GetFamilyManager()->DeleteFamily(param);
	else if( type == Journal::DELETE_INSPECT)
		IJournalComponent::GetInspectManager()->DeleteInspect(param);
	else if(type == Journal::DELETE_QUEEN_BEE)
		IJournalComponent::GetQueenBeeManager()->DeleteQueenBee(param);
	else {
		HTREEITEM item = m_NavigationWin.GetSelectedItem();
		long p = 0;
		if(item)
			p = (long)m_NavigationWin.GetItemData(item);
		if(type == Journal::NONE)
			m_state.m_nIdParameter = HIWORD(p);
		else
			m_state.m_nIdParameter = param;
		ChangeState(State((Journal::tabNumber)m_tabCtrl.GetCurSel(),
			(Journal::typeTreeItem)LOWORD(p), type));
	}
}
void CJournalTreeView::SelChange(){
	HTREEITEM item = m_NavigationWin.GetSelectedItem();
	long p = 0;
	if(item)
		p = (long)m_NavigationWin.GetItemData(item);
	m_state.m_nIdParameter = HIWORD(p);
	if(m_state.m_nState > Journal::VIEW_INSPECT_QBEE)
		return;
	ChangeState(State((Journal::tabNumber)m_tabCtrl.GetCurSel(),
		(Journal::typeTreeItem)LOWORD(p), Journal::NONE));
}
void CJournalTreeView::ChangeState(State state){
	m_pViewJornal->DestroyView();
	m_pViewJornal = m_viewFactory.GetViewJournal(state);
	m_pViewJornal->CreateView();
	m_pViewJornal->Focus();
}
